package com.luoxiang.socialandroid.adapter;


import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.luoxiang.socialandroid.R;
import com.luoxiang.socialandroid.activity.DetailsActivity;
import com.luoxiang.socialandroid.app.MyAppliaction;
import com.luoxiang.socialandroid.bean.Dynamic;
import com.luoxiang.socialandroid.bean.DynamicsData;
import com.luoxiang.socialandroid.utils.CustomClickListener;
import com.luoxiang.socialandroid.utils.LogUtils;
import com.luoxiang.lib_common.utils.NumTransferUtils;
import com.luoxiang.socialandroid.view.ExpandTextView;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description TODO
 * @systemUser Administrator
 * @Author LX
 * @Date 08-30-2022 周二 10:31
 */
public class RcCommunityAdapter extends RecyclerView.Adapter<RcCommunityAdapter.ViewHolder> {
    private List<Dynamic> list = new ArrayList<>();
    private DynamicsData data;
    //false 首次加载
    boolean flag = false;
    CommunityimgRvAdapter secondAdapter;
    LinearLayoutManager layoutManager;
    private View view;
    private Context context;

    public RcCommunityAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<Dynamic> list, DynamicsData data) {
        this.list = list;
        this.data = data;
    }

    public static interface OnAddClickListener {
        // true add; false cancel
        void onItemClicks(int position, int dylike); //传递boolean类型数据给activity
    }

    RcCommunityAdapter.OnAddClickListener onItemAddClick;

    public void setOnAddClickListener(RcCommunityAdapter.OnAddClickListener onItemAddClick) {
        this.onItemAddClick = onItemAddClick;
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.adapter_recommunity, parent, false);
        RcCommunityAdapter.ViewHolder holder = new RcCommunityAdapter.ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") int position) {
        if (!flag) {
            secondAdapter = new CommunityimgRvAdapter();
            layoutManager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
            //holder.rv_imgcode.addItemDecoration(new WaterFallItemDecoration(10, 0));
            holder.rv_imgcode.setLayoutManager(layoutManager);
            holder.rv_imgcode.setAdapter(secondAdapter);
        }


        Dynamic dylist = list.get(position);
        int online = dylist.getOnline();
        switch (online) {
            case 0:
                //离线隐藏
                holder.ic_onlinetorf.setVisibility(View.GONE);
                break;
            case 1:
                //勿扰隐藏
                holder.ic_onlinetorf.setVisibility(View.GONE);
                break;
            case 2:
                //在聊展示红点
                holder.ic_onlinetorf.setImageResource(R.mipmap.hongdianlixian);
                holder.ic_onlinetorf.setVisibility(View.VISIBLE);
                break;
            case 3:
                //在线展示绿点
                holder.ic_onlinetorf.setImageResource(R.mipmap.ic_online);
                holder.ic_onlinetorf.setVisibility(View.VISIBLE);
                break;
        }
        if (dylist.getIcon() != null) {
            Glide.with(MyAppliaction.getContext())
                    .load(data.getDomain() + dylist.getIcon())
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))//圆角半径
                    .into(holder.ic_header);
        }
        holder.ic_header.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                LogUtils.i("useridssssdddd", dylist.getUserId() + "qqq" + dylist.getId());
                Intent intent = new Intent(context, DetailsActivity.class);
                intent.putExtra("userid", dylist.getUserId());
                context.startActivity(intent);
            }

            @Override
            protected void onFastClick() {

            }
        });
        if (dylist.getTitle().isEmpty()) {
            holder.tv_title.setVisibility(View.GONE);
        } else {
            holder.tv_title.setText(dylist.getTitle());
            holder.tv_title.setVisibility(View.VISIBLE);
        }
        holder.likecount.setText(NumTransferUtils.INSTANCE.transfer(dylist.getLikesCount()));
        if (dylist.getUsername() != null) {
            holder.username.setText(dylist.getUsername());
        } else {
            holder.username.setText(R.string.mi_wunicheng);
        }

        if (dylist.isLike() == 0) {
            holder.ic_islike.setImageResource(R.mipmap.lovefalse);
        } else {
            holder.ic_islike.setImageResource(R.mipmap.lovatrue);
        }
        List<String> imgUrl = new ArrayList<>();
        int dylistType = dylist.getType();
        if (dylistType == 0) {
            imgUrl.addAll(dylist.getUrl());
        } else {
            imgUrl.add(dylist.getCover());
        }


        secondAdapter.setData(dylist.getId(), dylist.getUrl(), dylist.getUserId(), dylist.isFocus(), dylist.getIcon(), dylist.getUsername(), dylist.getTitle(), imgUrl, dylist.getType(), data.getDomain());

        holder.ic_islike.setOnClickListener(new CustomClickListener() {
            @Override
            protected void onSingleClick() {
                String count = dylist.getLikesCount();
                if (dylist.isLike() == 0) {
                    dylist.setLike(1);
                    dylist.setLikesCount(Integer.parseInt(count) + 1 + "");
                } else {
                    dylist.setLike(0);
                    dylist.setLikesCount(Integer.parseInt(count) - 1 + "");
                }
                onItemAddClick.onItemClicks(list.get(position).getId(), dylist.isLike());
            }

            @Override
            protected void onFastClick() {
                return;
            }

        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView ic_header, ic_islike, ic_onlinetorf;
        TextView likecount, username;
        RecyclerView rv_imgcode;
        ExpandTextView tv_title;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            ic_header = itemView.findViewById(R.id.ic_header);
            tv_title = itemView.findViewById(R.id.tv_title);
            ic_islike = itemView.findViewById(R.id.ic_islike);
            likecount = itemView.findViewById(R.id.likecount);
            rv_imgcode = itemView.findViewById(R.id.rv_imgcode);
            username = itemView.findViewById(R.id.username);
            ic_onlinetorf = itemView.findViewById(R.id.ic_onlinetorf);

        }
    }
}
